package com.jmpt.shiyan6;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView wv;
    private EditText etUrl;
    private Button btnBack, btnForward, btnGo;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // ✅ 必须在 setContentView() 之前调用
        getWindow().requestFeature(android.view.Window.FEATURE_PROGRESS);
        
        setContentView(R.layout.activity_main);

        wv = findViewById(R.id.webView);
        etUrl = findViewById(R.id.etUrl);
        btnBack = findViewById(R.id.btnBack);
        btnForward = findViewById(R.id.btnForward);
        btnGo = findViewById(R.id.btnGo);

        // 配置WebView
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setDomStorageEnabled(true);
        wv.setWebViewClient(new MyWebViewClient());
        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                setTitle("加载中...");
                setProgress(progress * 100);
                if (progress == 100)
                    setTitle(view.getTitle());
            }
        });

        // 初始加载
        wv.loadUrl("");//https://www.baidu.com
        etUrl.setText("");//https://www.baidu.com

        // 后退按钮
        btnBack.setOnClickListener(v -> {
            if (wv.canGoBack()) {
                wv.goBack();
            } else {
                Toast.makeText(MainActivity.this, "无法后退", Toast.LENGTH_SHORT).show();
            }
        });

        // 前进按钮
        btnForward.setOnClickListener(v -> {
            if (wv.canGoForward()) {
                wv.goForward();
            } else {
                Toast.makeText(MainActivity.this, "无法前进", Toast.LENGTH_SHORT).show();
            }
        });

        // 访问按钮
        btnGo.setOnClickListener(v -> {
            String url = etUrl.getText().toString().trim();
            if (URLUtil.isNetworkUrl(url)) {
                wv.loadUrl(url);
            } else {
                Toast.makeText(MainActivity.this, "请输入有效的网址", Toast.LENGTH_SHORT).show();
            }
        });
    }

    // 覆盖默认的URL加载行为
    private static class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    // 覆盖返回键，使用WebView的历史记录
    @Override
    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }
}